/*
 * @(#)UserDAOImpl.java 1.0 2012/03/06
 *
 * Copyright (c) 
 *
 * 
 */

package mock.ab_sd.daointerface;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import mock.ab_sd.common.valueobject.UserVO;

/**
 * UserDAOImpl class 
 * @author QuyGP
 * @created 15/8/2012
 */
public class UserDAOImpl implements UserDAO{
	
	/**
	 * get an ArrayList of UserVO
	 * @return ArrayList<UserVO>
	 * @author QuyGP
	 * @created 15/8/2012
	 */
	public ArrayList<UserVO> getArrayListOfUser() {
		
		// create an ArrayList
		ArrayList<UserVO> arrayList = new ArrayList<UserVO>();
		
		// catch exception of connection
		try {
			Connection connect = ConnectionFactory.getConnect();
			Statement statement = connect.createStatement();
			
			// create a query statement
			String query = "SELECT * FROM [User]";
			ResultSet result = statement.executeQuery(query);
				
			/* declare the variables corresponding to the fields
			 * of User table
			 */
			int userID = 0;
			String userName = "";
			String password = "";
			String role = "";
			String email = "";						
			boolean isActive = false;
				
			// to step through the records in the ResultSet
			while(result.next()){
					
				/* Assign the fields in User table to 
				 * the variables respectively
				 */
				userID = result.getInt(1);
				userName = result.getString(2);
				password = result.getString(3);
				role = result.getString(4);
				email = result.getString(5);
				isActive = result.getBoolean(7);			
					
				// create an UserVO object
				UserVO user = new UserVO();
					
				/* set the attributes of user to the variables 
				 * respectively
				 */
				user.setUserID(userID);
				user.setUserName(userName);
				user.setPassword(password);
				user.setRole(role);
				user.setEmail(email);			
				user.setActive(isActive);
					
				// add user to arrayList
				arrayList.add(user);
					
			}
				
			// close ResultSet
			result.close();	
				
			// close Statement
				statement.close();
				
			// close Connection
			connect.close();
				
		} catch (SQLException sqle) {
			System.out.println("User table not found");
										
		} catch (Exception e) {			
			System.out.println("Error of connection");
		}		
		
		return arrayList; // return ArrayList of UserVO
	}
		
}
